package com.example.controller;

import com.auth0.jwt.JWT;
import com.auth0.jwt.exceptions.AlgorithmMismatchException;
import com.auth0.jwt.exceptions.SignatureVerificationException;
import com.auth0.jwt.exceptions.TokenExpiredException;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.example.entity.User;
import com.example.service.UserService;
import com.example.utils.JWTutils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.security.SignatureException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 夏虫言冰
 * @date 2021/5/4 14:30
 */
@RestController
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("/user/login")
    public Map<String,Object> login(User user){
        log.info("用户名:[{}]",user.getName());
        log.info("密码：[{}]",user.getPassword());
        Map<String,Object> map= new HashMap<>();
        try {
            User login = userService.login(user);
            Map<String,String> payload = new HashMap<>();
            payload.put("id",login.getId());
            payload.put("name",login.getName());
            String token = JWTutils.getToken(payload);
            map.put("state",true);
            map.put("msg","认证成功");
            map.put("token",token);
        }catch (Exception e){
            map.put("state",false);
            map.put("msg",e.getMessage());
        }
        return  map;

    }

    @GetMapping("/hello")
    public String hello(){
        return "hello world ";
    }


    @PostMapping("/user/test")
    public Map<String,Object> test(HttpServletRequest request){
        Map<String,Object> map= new HashMap<>();
        String token = request.getHeader("token");
        DecodedJWT verify = JWTutils.verify(token);
        log.info("用户id:[{}]",verify.getClaim("id").asString());
        log.info("用户name: [{}]",verify.getClaim("name").asString());


        map.put("state",true);
        map.put("msg","认证成功");
        return map;
//        log.info("当前token：[{}]",token);
//        try {
//            DecodedJWT verify = JWTutils.verify(token);
//            map.put("state",true);
//            map.put("msg","认证成功");
//            return map;
//        }catch (SignatureVerificationException e){
//            e.printStackTrace();
//            map.put("msg","无效签名");
//        }catch (TokenExpiredException e){
//            e.printStackTrace();
//            map.put("msg","token过期");
//        }catch (AlgorithmMismatchException e){
//            e.printStackTrace();
//            map.put("msg","算法不一致");
//        }catch (Exception e){
//            e.printStackTrace();
//            map.put("msg","token无效");
//        }
//        map.put("state",false);
//        return  map;

    }
}
